//
//  JTableRefrashIndicater.m
//  IfengNews
//
//  Created by Ryan on 14-1-23.
//
//

#import "JTableRefrashIndicater.h"
#define DEGREES_TO_RADIANS(x) (x)/180.0*M_PI
#define RADIANS_TO_DEGREES(x) (x)/M_PI*180.0


@interface JTableRefrashIndicater ()

@property (nonatomic,unsafe_unretained) float theProgress;
@property (nonatomic,unsafe_unretained)JTableRefrashIndicaterState indicaterState;
@property (nonatomic,strong) NSArray *array;
@property (nonatomic,strong) UIImageView *animationImageView;
@property (nonatomic,strong) UIActivityIndicatorView *active;



@end

@implementation JTableRefrashIndicater

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor clearColor];
        self.indicaterState=JTableRefrashIndicaterStateProgress;
        NSMutableArray *array=[NSMutableArray array];
        for(int i=1;i<10;i++){
            UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
          
            [array sgrAddObject:image];
            
        }
        self.array=array;
        
        self.progressTintColor=RGB(0x70, 0x70, 0x70);
        
        self.animationImageView=[UIImageView new];
        self.animationImageView.frame=self.bounds;
       
        [self addSubview:self.animationImageView];
        self.animationImageView.hidden=YES;
       // self.loadImg=[UIImage imageNamed:@"02.png"];
        
      //  self.backgroundColor=[UIColor redColor];
        
//        self.active=[UIActivityIndicatorView new];
//        self.active.frame=self.bounds;
//        [self addSubview:self.active];
//        self.active.hidesWhenStopped=YES;
       
    }
    return self;
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    self.animationImageView.frame=self.bounds;

   

}


- (void)setProgress:(float)progress{
    if(self.indicaterState!=JTableRefrashIndicaterStateProgress) return;
    if(progress>1.0f)self.theProgress=1.0f;
    if(progress<0.0f)self.theProgress=0.0f;
    self.theProgress=progress;
    
    
    //[self addSubview:<#(UIView *)#>];
    [self setNeedsDisplay];

}

- (void)updateCSS{
    //self.progressTintColor = ICurrentTheme.listStyle.listReflashTextColor;
//    [self setNightBlock:^(UIView *me, BOOL isDay) {
//        ((JTableRefrashIndicater *)me).progressTintColor=
//        isDay?[UIColor colorWithRed:0x86/255.0f green:0x86/255.0f blue:0x86/255.0f alpha:1.0f]:
//        [UIColor colorWithRed:0x63/255.0f green:0x6e/255.0f blue:0x7c/255.0f alpha:1.0f];
//        
//    }];
    
}


- (void)starInfinitAnimation{
  //  [self.active startAnimating];
    self.animationImageView.hidden=NO;
    self.animationImageView.image=[UIImage animatedImageWithImages:self.array duration:(1.0f / 24.0f)*[self.array count]];

    

}

- (void)stopInfinitAnimation{
   // [self.active stopAnimating];
    self.animationImageView.hidden=YES;
    self.animationImageView.image=nil;
    [self.layer removeAnimationForKey:@"indeterminateAnimation"];
}


- (void)setLogoState:(JTableRefrashIndicaterState )aindicaterState{
    self.indicaterState=aindicaterState;
     //  [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);
    CGContextSetStrokeColorWithColor(ctx, [UIColor clearColor].CGColor);
    CGContextFillRect(ctx, rect);
    
    
    if(self.indicaterState==JTableRefrashIndicaterStateProgress){
        
        if(self.theProgress>=1.0f){
            [self.array[0] drawInRect:rect];
        }else{
            CGContextSetStrokeColorWithColor(ctx, self.progressTintColor.CGColor);
            CGContextStrokeEllipseInRect(ctx, CGRectInset(self.bounds, 1, 1));
            CGPoint center = CGPointMake(self.bounds.origin.x+self.bounds.size.width/2, self.bounds.origin.y+self.bounds.size.height/2);
            CGContextMoveToPoint(ctx, center.x, center.y);
            CGContextSetFillColorWithColor(ctx, self.progressTintColor.CGColor);
            
            CGContextAddArc(ctx, center.x, center.y, self.bounds.size.width/2-1,  DEGREES_TO_RADIANS(-90), DEGREES_TO_RADIANS(-90+self.theProgress*360), 0);
            CGContextFillPath(ctx);

        }
    }else if(self.indicaterState==JTableRefrashIndicaterStateImg){
        [self.array[0] drawInRect:rect];
    }else if(self.indicaterState==JTableRefrashIndicaterStateImgLoading){
        [self.array[0] drawInRect:rect];
    }else if(self.indicaterState==JTableRefrashIndicaterStateImgFail){
       
        [self.array[0] drawInRect:rect];
    }

    
    

}


@end
